<html>
<head>
    <title>Resultados</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
<?php
require 'functions.php';

$MAQUINATOTAL = "SELECT count(*) FROM maquinacliente";
$PERCENT = "cast((count(*)*100/(".$MAQUINATOTAL.")) AS DECIMAL(8,2)) AS `Porcentaje`";

$MAQUINA=$_GET['maquina'];

if ($_GET['filtroMaqPercent']=="MaqPercentV"){

    $QUERY  = "SELECT VE.`nombre` as Nombre , VE.`apellido` as Apellido, MA.`nombre` as `Nombre de Maquina`, MA.`marca` as `Marca de Maquina`, (sum(MC.`contador`)*100/ SQM.`contador`) as `Porcentaje De Maquinas` ";
    $QUERY .= "FROM `cliente` as CL ";
    $QUERY .= "INNER JOIN `vendedor` as VE ON CL.`vendedor_id`=VE.`vendedor_id` ";
    $QUERY .= "INNER JOIN `maquinacliente` as MC ON CL.`cliente_id`=MC.`cliente_id` ";
    $QUERY .= "INNER JOIN `maquina` as MA ON MC.`maquina_id`=MA.`maquina_id` ";
    $QUERY .= "INNER JOIN (SELECT MA.`nombre`, sum(MC.`contador`) as contador ";
    $QUERY .= "FROM `cliente` as CL ";
    $QUERY .= "INNER JOIN `maquinacliente` as MC ON CL.`cliente_id`=MC.`cliente_id` ";
    $QUERY .= "INNER JOIN `maquina` as MA ON MC.`maquina_id`=MA.`maquina_id` ";
    $QUERY .= "WHERE MA.`maquina_id`=$MAQUINA ";
    $QUERY .= "GROUP BY MA.`nombre`) as SQM ON SQM.`nombre`=MA.`nombre` ";
    $QUERY .= "WHERE MA.`maquina_id`=$MAQUINA ";
    $QUERY .= "GROUP BY VE.`vendedor_id` ";

}elseif ($_GET['filtroMaqPercent']=="MaqPercentZ"){

    $QUERY  = "SELECT ZO.`zona_id` as Nombre, MA.`nombre` as `Nombre de Maquina`, MA.`marca` as `Marca de Maquina`, (sum(MC.`contador`)*100/ SQM.`contador`) as `Porcentaje De Maquinas` ";
    $QUERY .= "FROM `cliente` as CL ";
    $QUERY .= "INNER JOIN `zona` as ZO ON CL.`zona_id`=ZO.`zona_id` ";
    $QUERY .= "INNER JOIN `maquinacliente` as MC ON CL.`cliente_id`=MC.`cliente_id` ";
    $QUERY .= "INNER JOIN `maquina` as MA ON MC.`maquina_id`=MA.`maquina_id` ";
    $QUERY .= "INNER JOIN (SELECT MA.`nombre`, sum(MC.`contador`) as contador ";
    $QUERY .= "FROM `cliente` as CL ";
    $QUERY .= "INNER JOIN `maquinacliente` as MC ON CL.`cliente_id`=MC.`cliente_id` ";
    $QUERY .= "INNER JOIN `maquina` as MA ON MC.`maquina_id`=MA.`maquina_id` ";
    $QUERY .= "WHERE MA.`maquina_id`=$MAQUINA ";
    $QUERY .= "GROUP BY MA.`nombre`) as SQM ON SQM.`nombre`=MA.`nombre` ";
    $QUERY .= "WHERE MA.`maquina_id`=$MAQUINA ";
    $QUERY .= "GROUP BY ZO.`zona_id` ";

}



/*Muestra la consulta bonita*/
sqlQueryPrettyParser($QUERY);

/*Efectua la consulta*/
$DATA = sqlQueryToDB($QUERY);

/*Muestra la tabla de resultados*/
if ($DATA->num_rows < 1) {
    echo "No hay resultados para armar una tabla<br><br>";
} else {
    echo sqlDataToHtmlTable($DATA);
}

?>
<br>
<input type="button" onclick="history.back()" name="Regresar" value="Regresar">
</body>
</html>
